;(function(){
	var validates = {rules: {
						username:{vd_azAZ09:[6,20]},
						password:{vd_azAZ09:[6,20]},
						realName:{rangelength:[2,10]},
					}
	};
	var table = new Table('#userTable',{
		url: '/admin/api/user',
		toolbar: "#userTableBTNs", //工具列
		modal: "#saveUser",
		uniqueId: 'username',
		columns: [
			{checkbox: true},
			{field:'username',title:'账号'},
			{field:'realname',title:'姓名'},
			{field:'loginLastime',title:'最后登录'},
			{field:'enabled',title:'状态',formatter: function(value,row){
				return value ? '启用':'禁用';
			}},
			{field:'createTime',title:'注册时间'},
		]
	})
		.add(validates)
		.del()
	    .upd(validates);

 	//设置用户角色 VIEW
 	function handleUserAddRole(){
 		var row=table.selected();
 		if(row){
 			Utils.show($('#addUserRole'),function(modal,$){
 	   		    var $select = $('.select-roles',modal);
 		   		$.ajax({
						url:'/admin/api/user/'+row.username+'/role',
						type:'GET',
 				   		success:function(respEntity){
 		   					var roles = respEntity.content.roles;
 		   					var selected = respEntity.content.selected
 				   			for(var i=0;i<roles.length;i++){
 				   				$select.append('<option value="'+roles[i].id+'">'+roles[i].groupName+'</option>');
 				   			}
 				   			$select.multiSelect({
 				   				selectableHeader: "<div class='custom-header'>选择角色</div>",
 				   			     selectionHeader: "<div class='custom-header'>已有角色</div>",
 				   			    selectableFooter: "<div class='custom-header'>选择角色</div>",
 				   			     selectionFooter: "<div class='custom-header'>已有角色</div>"});
 				   			for(var i=0;i<selected.length;i++){
 				   				selected[i]=selected[i]+'';
 				   			}
 				   			$select.multiSelect('select',selected);
 				   			$('ul.ms-list',modal).css({height:'400px'});
 				   		}
 					});
 				$('.sure',modal).on('click',function(){
 					userAddRole(row.username,$select.val());
 				});
 			});
 		}else Utils.alert('警告','请选择一行记录!');
 	}
 	function userAddRole(username,roleIds){
 		$.ajax(
 			{
				url: '/admin/api/user/'+username+'/role',
				type: 'PUT',
				data: {roleIds:roleIds},
				success:function(respEntity){
					if(respEntity.content == true)
						Utils.alert('操作提醒','操作成功!');
				},
		});
 	}
 	//设置用户权限VIEW
 	function handleUserAddRight(){
 		var row=table.selected();
 		if(row){
            Utils.showRemote({url:"/admin/api/user/"+row.username+"/modal_authority.html"},function ($modal) {
                $('input:checkbox',$modal).uniform();
                $('.sure',$modal).on('click',function(){
                    var authorities = [];
                    $('input[type="checkbox"]:checked',$modal).each(function (i) {
                        authorities.push($(this).val());
                    })
                    $.ajax({
                        url: '/admin/api/user/'+row.username+'/right',
                        type: 'PUT',
                        data: {rightIds:authorities},
                        success:function(respEntity){
                            if(respEntity.content == true)
                                Utils.alert('操作提醒','操作成功!');
                        },
                    });
                });
            })
 		}else Utils.alert('警告','请选择一行记录!');
 	}
 	
	$('#userTableBTNs button.addUserRight').on('click',function(){handleUserAddRight();});//add userRight
	$('#userTableBTNs button.addUserRole').on('click',function(){handleUserAddRole();});  //add userRole

})();